package JUC.signal;

/**
 * @author mubei
 * @date 2021/11/23
 * @description
 */
public class LazyMan {

    private LazyMan(){
        System.out.println(Thread.currentThread().getName()+"ok");
    }

    // 防止指令重排
    private volatile static LazyMan lazyMan;

    public static LazyMan getInstance() {
        // 双重检测  懒汉式单列
        if(lazyMan == null){
            synchronized(LazyMan.class){
                if(lazyMan == null){
                    lazyMan = new LazyMan();
                }
            }
        }

        return lazyMan;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                /*
                * 不是原子性操作
                * 1.分配内存空间
                * 2.执行构造方法
                * 3.把这个对象指向这个空间
                * */
                lazyMan.getInstance();
            }).start();
        }
    }
}
